import { selectKNTracks, selectKNgMapTracks } from '@/services/corelle/myOrder';
import { getTracking } from '@/services/myOrder/trail';

import prompt from '@/components/Prompt';
import { getPageSize } from '@/utils/common';

export default {
  namespace: 'myOrder',

  state: {
    KNTracksList: {},
    KNTracksOne:{},
  },

  effects: {
    // 一、查询订单管理列表
    *selectKNTracks({ payload, callback }, { call, put }) {
      const response = yield call(selectKNTracks, payload);
      const {
        code,
        data: { list, pageNum, pages, total, pageIndex },
      } = response;
      const data = response.data || {};
      yield put({
        type: 'saveAllValus',
        payload: {
          KNTracksList: {
            pagination: {
              //   current: pageNum,
              currentPage: pageNum, //currentPage区别不同平台传参数
              pageSize: getPageSize(),
              total,
              pages,
            },
            list,
          },
        },
      });
      callback && callback(list);
    },
    *selectKNgMapTracks({ payload, callback }, { call }) {
      const response = yield call(selectKNgMapTracks, payload);
      const { code, data, message } = response;
      if (code !== 0) return;
      callback && callback(data);
    },

    //二、轨迹
    *getTracking({ payload }, { call, put }) {
      const response = yield call(getTracking, payload);
      const { data, code } = response;
      yield put({
        type: 'list',
        payload: {
          trackData: data,
        },
      });
    },
    *getTrackingOne({ payload }, { call, put }) {
      const response = yield call(selectKNTracks, payload);
      const { data, code } = response;
      yield put({
        type: 'list',
        payload: {
          KNTracksOne: data.list[0],
        },
      });
    },

    /**
     * 设置表单参数
     */
    *allValus({ payload }, { _, put }) {
      yield put({
        type: 'saveAllValus',
        payload,
      });
    },
  },

  reducers: {
    //设置表单参数
    saveAllValus(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    detail(state, { payload }) {
      let param = '';
      for (let k in payload) {
        param = k;
      }
      return {
        ...state,
        [param]: { ...state[param], ...payload[param] },
      };
    },
    list(state, { payload }) {
      let param = '';
      for (let k in payload) {
        param = k;
      }
      return {
        ...state,
        [param]: { ...payload[param] },
      };
    },
  },
};
